<?php

class Sms extends M_Controller
{
    public function send_captcha()
    {
        $type = $this->input->get('type');
        $phone = trim($_POST['phone']);
        $types = ['register', 'change_password', 'login','change_pay','withdrawal'];
        if (!is_valid('phone', $phone)) {
            $this->json(0, '请输入正确的手机号');
        }
        if (!in_array($type, $types)) {
            $this->json(0, '验证码类型不支持');
        } elseif ($type == 'login') {
            $memberInfo = $this->models('member')->uidInfo(['username' => trim($phone)], 'uid');
            if (empty($memberInfo)) {
                $this->json(0, '账户不存在');
            }
        }
        if (
            $captcha = $this->models('system/sms')->createCaptcha($phone, $type)
            and $this->models('system/sms')->provider('tencent')->templateSend($phone, [$captcha])
        ) {
            $this->json(1, '发送成功');
        } else {
            $this->json(0, '操作繁忙');
        }
    }
}
